﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class System_Pages_Content_BlogManager : SiteFox.Base.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            this.LoadBlog();
        }
    }

    #region ControlsEvents
    protected void imb_Blog_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_Blog_AddOrEdit.Visible = true;
        lab_Blog_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_Blog_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
        hf_Blog_SelectedId.Value = "";
    }
    protected void lib_Blog_AddOrEdit_Close_Click(object sender, EventArgs e)
    {
        this.ResetBlogAddOrEdit();
        lsb_Blog_AddOrEdit_Categories_Value.ClearSelection();
    }
    protected void lib_Blog_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Model.Blog _Blog = new SiteFox.Business.Model.Blog();
        if (hf_Blog_SelectedId.Value != "")
        {
            _Blog = SiteFox.Business.Manager.Blog.GetByID(Convert.ToInt32(hf_Blog_SelectedId.Value));
        }
        _Blog.Description = ucc_Blog_AddOrEdit_Description.Value;
        _Blog.Name = ucc_Blog_AddOrEdit_Name.Value;

        if (hf_Blog_SelectedId.Value == "")
        {
            _Blog.Created = DateTime.Now;
            _Blog.CreatedBy = Page.User.Identity.Name;
            SiteFox.Business.Manager.Blog.Create(_Blog);
        }
        else
        {
            _Blog.Modified = DateTime.Now;
            _Blog.ModifiedBy = Page.User.Identity.Name;
            SiteFox.Business.Manager.Blog.Edit(_Blog);
        }
        this.ResetBlogAddOrEdit();
        this.LoadBlog();
        lsb_Blog_AddOrEdit_Categories_Value.ClearSelection();
    }
    protected void lib_Blog_AddOrEdit_Categories_Add_Click(object sender, EventArgs e)
    {
        lsb_Blog_AddOrEdit_Categories_Value.ClearSelection();
        lib_Blog_AddOrEdit_Categories_Add.Visible = false;
        lib_Blog_AddOrEdit_Categories_Edit.Visible = false;
        lib_Blog_AddOrEdit_Categories_Delete.Visible = false;
        pan_Blog_AddOrEdit_Categories_AddOrEdit.Visible = true;
    }
    protected void lib_Blog_AddOrEdit_Categories_Edit_Click(object sender, EventArgs e)
    {
        if (lsb_Blog_AddOrEdit_Categories_Value.SelectedValue != "") 
        { 
            lib_Blog_AddOrEdit_Categories_Add.Visible = false;
            lib_Blog_AddOrEdit_Categories_Edit.Visible = false;
            lib_Blog_AddOrEdit_Categories_Delete.Visible = false;
            pan_Blog_AddOrEdit_Categories_AddOrEdit.Visible = true;
            tb_Blog_AddOrEdit_Categories_AddOrEdit_Value.Text = lsb_Blog_AddOrEdit_Categories_Value.SelectedItem.Text;
        }
    }
    protected void lib_Blog_AddOrEdit_Categories_Delete_Click(object sender, EventArgs e)
    {
        if (lsb_Blog_AddOrEdit_Categories_Value.SelectedValue != "")
        { 
            SiteFox.Business.Manager.BlogCategory.Delete(Convert.ToInt32(lsb_Blog_AddOrEdit_Categories_Value.SelectedValue));
            this.LoadBlogCagetoriesInListBox();
        }
    }
    protected void lib_Blog_AddOrEdit_Categories_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Model.BlogCategory _BlogCategory = new SiteFox.Business.Model.BlogCategory();
        if (lsb_Blog_AddOrEdit_Categories_Value.SelectedValue != "") 
        {
            _BlogCategory = SiteFox.Business.Manager.BlogCategory.GetByID(Convert.ToInt32(lsb_Blog_AddOrEdit_Categories_Value.SelectedValue));
        }
        _BlogCategory.Description = "";
        _BlogCategory.Title = tb_Blog_AddOrEdit_Categories_AddOrEdit_Value.Text;

        if (lsb_Blog_AddOrEdit_Categories_Value.SelectedValue == "")
        {
            _BlogCategory.Created = DateTime.Now;
            _BlogCategory.CreatedBy = User.Identity.Name;
            _BlogCategory.ID_Blog = Convert.ToInt32(hf_Blog_SelectedId.Value);
            SiteFox.Business.Manager.BlogCategory.Create(_BlogCategory);
        }
        else 
        {
            _BlogCategory.Modified = DateTime.Now;
            _BlogCategory.ModifiedBy = User.Identity.Name;
            SiteFox.Business.Manager.BlogCategory.Edit(_BlogCategory);
        }
        lib_Blog_AddOrEdit_Categories_Add.Visible = true;
        lib_Blog_AddOrEdit_Categories_Edit.Visible = false;
        lib_Blog_AddOrEdit_Categories_Delete.Visible = false;
        pan_Blog_AddOrEdit_Categories_AddOrEdit.Visible = false;
        tb_Blog_AddOrEdit_Categories_AddOrEdit_Value.Text = "";
        this.LoadBlogCagetoriesInListBox();
    }
    protected void lib_Blog_AddOrEdit_Categories_AddOrEdit_Cancel_Click(object sender, EventArgs e)
    {
        lib_Blog_AddOrEdit_Categories_Add.Visible = true;
        lib_Blog_AddOrEdit_Categories_Edit.Visible = true;
        lib_Blog_AddOrEdit_Categories_Delete.Visible = true;
        pan_Blog_AddOrEdit_Categories_AddOrEdit.Visible = false;
        tb_Blog_AddOrEdit_Categories_AddOrEdit_Value.Text = "";
    }
    
    //----- Panel Blog Delete -----//
    protected void lib_Blog_Delete_Close_Click(object sender, EventArgs e)
    {
        this.ResetBlogDelete();
    }
    protected void lib_Blog_Delete_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Manager.Blog.Delete(Convert.ToInt32(hf_Blog_SelectedId.Value));
        this.ResetBlogDelete();
        this.LoadBlog();
    }
    
    //----- Blog GridView ActionButtons -----//
    protected void lib_Blog_Posts_Click(object sender, EventArgs e)
    {
        hf_Blog_SelectedId.Value = ((LinkButton)(sender)).CommandArgument;
        this.LoadBlogPost(Convert.ToInt32(hf_Blog_SelectedId.Value));
    }
    protected void lib_Blog_Edit_Click(object sender, EventArgs e)
    {
        hf_Blog_SelectedId.Value = ((LinkButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_Blog_SelectedId.Value);

        var _Blog = SiteFox.Business.Manager.Blog.GetByID(_ID);
        pan_Blog_AddOrEdit.Visible = true;
        lab_Blog_AddOrEdit_Title.Text = Resources.dcCommon.dc_Edit;
        lib_Blog_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        ucc_Blog_AddOrEdit_Description.Value = _Blog.Description;
        ucc_Blog_AddOrEdit_Name.Value = _Blog.Name;
        this.LoadBlogCagetoriesInListBox();
    }
    protected void lib_Blog_Delete_Click(object sender, EventArgs e)
    {
        hf_Blog_SelectedId.Value = ((LinkButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_Blog_SelectedId.Value);
        pan_Blog_Delete.Visible = true;
    }

    //----- Panel BlogPost AddOrEdit -----//
    protected void imb_BlogPost_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_BlogPost_AddOrEdit.Visible = true;
        lab_BlogPost_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_BlogPost_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
        this.LoadBlogCagetoriesInLDropDownList();
    }
    protected void lib_BlogPost_AddOrEdit_Close_Click(object sender, EventArgs e)
    {
        this.ResetBlogPostAddOrEdit();
    }
    protected void lib_BlogPost_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Model.BlogPost _BlogPost = new SiteFox.Business.Model.BlogPost();
        if (hf_BlogPost_AddOrEdit_Id.Value != "")
        {
            _BlogPost = SiteFox.Business.Manager.BlogPost.GetByID(Convert.ToInt32(hf_BlogPost_AddOrEdit_Id.Value));
        }
        _BlogPost.ID_BlogCategory = Convert.ToInt32(ddl_BlogPost_AddOrEdit_Cateogry_Value.SelectedValue);
        _BlogPost.Description = ucc_BlogPost_AddOrEdit_Description.Value;
        _BlogPost.Title = ucc_BlogPost_AddOrEdit_Title.Value;
        _BlogPost.Text = ucc_BlogPost_AddOrEdit_Text.Value;

        if (hf_BlogPost_AddOrEdit_Id.Value == "")
        {
            var _BlogPosts = SiteFox.Business.Manager.BlogPost.GetByBlog(Convert.ToInt32(hf_Blog_SelectedId.Value));
            _BlogPost.ID_Blog = Convert.ToInt32(hf_Blog_SelectedId.Value);
            _BlogPost.Created = DateTime.Now;
            _BlogPost.CreatedBy = Page.User.Identity.Name;
            SiteFox.Business.Manager.BlogPost.Create(_BlogPost);
        }
        else
        {
            _BlogPost.Modified = DateTime.Now;
            _BlogPost.ModifiedBy = Page.User.Identity.Name;
            SiteFox.Business.Manager.BlogPost.Edit(_BlogPost);
        }
        this.ResetBlogPostAddOrEdit();
        this.LoadBlogPost(Convert.ToInt32(hf_Blog_SelectedId.Value));
    }
    
    //----- Panel BlogPost Delete -----//
    protected void imb_BlogPost_Delete_Close_Click(object sender, ImageClickEventArgs e)
    {
        this.ResetBlogPostDelete();
    }
    protected void lib_BlogPost_Delete_Ok_Click(object sender, EventArgs e)
    {
        int _ID = Convert.ToInt32(hf_BlogPost_Delete_Id.Value);
        SiteFox.Business.Manager.BlogPost.Delete(_ID);
        this.ResetBlogPostDelete();
        this.LoadBlogPost(Convert.ToInt32(hf_Blog_SelectedId.Value));
        this.BlogModified();
    }
    
    //----- BlogPost GridView ActionButtons -----//
    protected void lib_BlogPost_Edit_Click(object sender, EventArgs e)
    {
        hf_BlogPost_AddOrEdit_Id.Value = ((ImageButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_BlogPost_AddOrEdit_Id.Value);
        var _BlogPost = SiteFox.Business.Manager.BlogPost.GetByID(_ID);
        this.LoadBlogCagetoriesInLDropDownList();
        pan_BlogPost_AddOrEdit.Visible = true;
        lab_BlogPost_AddOrEdit_Title.Text = Resources.dcCommon.dc_Edit;
        lib_BlogPost_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        ucc_BlogPost_AddOrEdit_Description.Value = _BlogPost.Description;
        ucc_BlogPost_AddOrEdit_Title.Value = _BlogPost.Title;
        ucc_BlogPost_AddOrEdit_Text.Value = _BlogPost.Text;
        if (_BlogPost.ID_BlogCategory != null) 
        { 
            ddl_BlogPost_AddOrEdit_Cateogry_Value.SelectedValue = _BlogPost.ID_BlogCategory.ToString();
        }
    }
    protected void lib_BlogPost_Delete_Click(object sender, EventArgs e)
    {
        hf_BlogPost_Delete_Id.Value = ((ImageButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_BlogPost_Delete_Id.Value);
        pan_BlogPost_Delete.Visible = true;
    }
    #endregion

    #region Methods
    void LoadBlog()
    {
        gv_Blog.DataSource = SiteFox.Business.Manager.Blog.GetAll();
        gv_Blog.DataBind();
    }
    void LoadBlogPost(int _BlogId)
    {
        pan_BlogPost_Title.Visible = true;
        var _Blog = SiteFox.Business.Manager.Blog.GetByID(_BlogId);
        lab_BlogPost_Title.Text = Resources.dcCommon.dc_PostsFor + " <strong>" + _Blog.Name + "</strong>";
        gv_BlogPost.DataSource = SiteFox.Business.Manager.BlogPost.GetByBlog(_BlogId);
        gv_BlogPost.DataBind();
    }
    void ResetBlogAddOrEdit()
    {
        pan_Blog_AddOrEdit.Visible = false;
        lab_Blog_AddOrEdit_Title.Text = "";
        lib_Blog_AddOrEdit_Ok.Text = "";
        hf_Blog_SelectedId.Value = "";
        ucc_Blog_AddOrEdit_Description.Value = "";
        ucc_Blog_AddOrEdit_Name.Value = "";
    }
    void ResetBlogDelete()
    {
        pan_Blog_Delete.Visible = false;
        hf_Blog_SelectedId.Value = "";
    }
    void ResetBlogPostAddOrEdit()
    {
        pan_BlogPost_AddOrEdit.Visible = false;
        lab_BlogPost_AddOrEdit_Title.Text = "";
        lib_BlogPost_AddOrEdit_Ok.Text = "";
        hf_BlogPost_AddOrEdit_Id.Value = "";
        ucc_BlogPost_AddOrEdit_Description.Value = "";
        ucc_BlogPost_AddOrEdit_Title.Value = "";
        ucc_BlogPost_AddOrEdit_Text.Value = "";
        ddl_BlogPost_AddOrEdit_Cateogry_Value.Items.Clear();
    }
    void ResetBlogPostDelete()
    {
        pan_BlogPost_Delete.Visible = false;
        hf_BlogPost_Delete_Id.Value = "";
    }
    void BlogModified()
    {
        var _Blog = SiteFox.Business.Manager.Blog.GetByID(Convert.ToInt32(hf_Blog_SelectedId.Value));
        _Blog.Modified = DateTime.Now;
        _Blog.ModifiedBy = Page.User.Identity.Name;
        SiteFox.Business.Manager.Blog.Edit(_Blog);
        this.LoadBlog();
    }
    void LoadBlogCagetoriesInListBox() 
    {
        lsb_Blog_AddOrEdit_Categories_Value.Items.Clear();
        var _BlogCategories = SiteFox.Business.Manager.BlogCategory.GetByBlog(Convert.ToInt32(hf_Blog_SelectedId.Value));
        lsb_Blog_AddOrEdit_Categories_Value.DataSource = _BlogCategories;
        lsb_Blog_AddOrEdit_Categories_Value.DataTextField = "Title";
        lsb_Blog_AddOrEdit_Categories_Value.DataValueField = "ID";
        lsb_Blog_AddOrEdit_Categories_Value.DataBind();
    }
    void LoadBlogCagetoriesInLDropDownList()
    {
        ddl_BlogPost_AddOrEdit_Cateogry_Value.Items.Clear();
        var _BlogCategories = SiteFox.Business.Manager.BlogCategory.GetByBlog(Convert.ToInt32(hf_Blog_SelectedId.Value));
        ddl_BlogPost_AddOrEdit_Cateogry_Value.DataSource = _BlogCategories;
        ddl_BlogPost_AddOrEdit_Cateogry_Value.DataTextField = "Title";
        ddl_BlogPost_AddOrEdit_Cateogry_Value.DataValueField = "ID";
        ddl_BlogPost_AddOrEdit_Cateogry_Value.DataBind();
    }
    #endregion


    //----- Panel Blog AddOrEdit -----//

}